package ch.elca.lol.puissance4.board;

import ch.elca.lol.puissance4.game.Token;

/** BoardData using Token[] */
public final class LinMat implements BoardData {
	protected final Token[] tokens;
	private final int w;
	private final int h;
	
	public LinMat(int w, int h) {
		this(new Token[w * h], w, h);
	}
	
	protected LinMat(Token[] tokens, int w, int h) {
		this.tokens = tokens;
		this.w = w;
		this.h = h;
	}
	
	public int getWidth() {
		return w;
	}
	
	public int getHeight() {
		return h;
	}
	
	public LinMat set(int x, int y, Token t) {
		Token[] copy = tokens.clone();
		copy[x + y * w] = t;
		return new LinMat(copy, w, h);
	}
	
	public Token get(int x, int y) {
		return tokens[x + y * w];
	}
	
	public boolean isNull(int x, int y) {
		return tokens[x + y * w] == null;
	}
	
	public int bottomOfCol(int x) {
		for(int y = 0; y < h; y++) {
			if(isNull(x, y))
				return y;
		}
		
		return h;
	}
}
